package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Student;
import com.woniuxy.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


/**
 * (Student)表控制层
 *学生表对应接口
 * @author zjx
 * @since 2021-05-14 19:42:03
 */
@CrossOrigin
@Api(value="学生信息表接口",description = "用于对学生信息表实现CRUD")
@RestController
@RequestMapping("student")
public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;
    /**
     * 查看试卷
     *
     */
    @GetMapping("/findExamPaper")
    @ApiOperation("查看试卷 传参试卷ID")
    public ResponseEntity findExamPaper(int studentId,int examId){
        Map<String, Object> examPaperJson= this.studentService.findExamPaper(studentId,examId);

        return new ResponseEntity(200,"成功",examPaperJson);
    }

    /**
     * 询查 一定时间段内  积分排行榜数据实现渲染
     * 前端回传的map中包含的参数
     *  beginTime 开始时间
     *  overTime 结束时间
     *    startPage 起始页
     *   pageSize 页大小
     * @return RequestEntity集合
     */

    @PostMapping("leaderBoard")
    @ApiOperation("积分排行榜页面渲染")
    public ResponseEntity leaderBoard(@RequestBody Map<String,String> map) {

        //将json中的分页信息转换为int
        int startPage = Integer.parseInt(map.get("startPage"));
        int pageSize = Integer.parseInt(map.get("pageSize"));

        //去除json中的sql语句参数,开始时间和结束时间
        String beginTime = map.get("beginTime");
        String overTime = map.get("overTime");

        PageInfo<Map<String, String>> mapPageInfo = this.studentService.leaderBoard(startPage, pageSize, beginTime, overTime);
        return new ResponseEntity(200,"成s功",mapPageInfo);
    }

        /**
         * 通过学生姓名模糊查询员工档案    以及  页面渲染数据
         *
         * @param startPage 起始页
         * @param pageSize 每页几条
         *  @param id 学生主键
         * @return ResponseEntity
         */
    @GetMapping("integralSubsidiary")
    @ApiOperation("积分详情页面信息")
    public ResponseEntity integralSubsidiary(@ApiParam("起始页") Integer startPage,
                                             @ApiParam( "页大小")Integer pageSize,
                                             @ApiParam( "学生主键id")Integer id){
        PageInfo<Map<String,Object>> pageInfo= this.studentService.integralSubsidiary(startPage, pageSize, id);
        return new ResponseEntity(200,"成功",pageInfo);

    }
    /**
     * 通过学生姓名模糊查询员工档案    以及  页面渲染数据
     *
     * @param startPage 起始页
     * @param pageSize 每页几条
     *  @param id 学生主键
     * @return ResponseEntity
     */
    @GetMapping("viewDetails")
    @ApiOperation("查看详情页面渲染")
    public ResponseEntity viewDetails(@ApiParam("起始页") Integer startPage,
                                      @ApiParam( "页大小")Integer pageSize,
                                      @ApiParam( "学生主键id")Integer id){
        PageInfo<Map<String, Object>> mapPageInfo = this.studentService.viewDetails(startPage, pageSize, id);
        return new ResponseEntity(200,"成功",mapPageInfo);
    }
    /**
     * 通过学生姓名模糊查询员工档案    以及  页面渲染数据
     *
     * @param startPage 起始页
     * @param pageSize 每页几条
     *  @param name 模糊查询的学生姓名
     * @return map
     */
    @GetMapping("fuzzySelectStudentTestIntegral")
    @ApiOperation("员工档案前端页面渲染 以及 模糊查询请求")
    public  ResponseEntity fuzzySelectStudentTestIntegral(@ApiParam("起始页") Integer startPage,
                                                             @ApiParam( "页大小") Integer pageSize,
                                                             @ApiParam("模糊查询用的学生姓名") String name ){
        System.out.println(startPage+"*******"+pageSize);
       PageInfo<Map<String, String>> mapPageInfo1 = this.studentService.fuzzySelectStudentTestIntegral(startPage, pageSize, name);
        return new ResponseEntity(200,"页面渲染数据请求成功",mapPageInfo1);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("selectOne")
    public ResponseEntity selectOne(@ApiParam("学生表主键") Integer id) {
        Student selectStudent = this.studentService.queryById(id);
        return new ResponseEntity(200,"查询学生信息成功",selectStudent);
    }


    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @ApiOperation("实现对学生单条数据的新增")
    @PostMapping("insertOne")
    public ResponseEntity insert(@ApiParam("学生实例对象") @RequestBody Student student){
        Student insertStudent = this.studentService.insert(student);
        return new ResponseEntity(200,"新增学生信息成功",insertStudent);
    }
    /**
     * 穿的是实体信息 修改数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @ApiOperation("实现对学生实体信息的数据修改")
    @PostMapping("updateOne")
    public ResponseEntity update(@ApiParam("学生实例对象") @RequestBody Student student){
        Student updateStudent = this.studentService.update(student);
        return new ResponseEntity(200,"修改学生信息成功",updateStudent);
    }



    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @ApiOperation("通过主键实现对学生单条数据的删除")
    @GetMapping("deleteOne")
    public ResponseEntity delete(@ApiParam("主键") int id){
        boolean deleteStudent = this.studentService.deleteById(id);
        if(deleteStudent){
            return new ResponseEntity(200,"删除学生信息成功",deleteStudent);
        }else {
            return new ResponseEntity(200,"删除失败",null);
        }


    }

}
